package com.rcd.viatornisl.android.model;

import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Base64;
import android.util.Log;

public class UserManager extends BaseManager {
	
	private final String USER = "users/";
	private final String USER_JSONLIST = "jsonlist/";
	private final String USER_JSONVIEW = "jsonview/";	
	
	public UserManager() {
		// Setup the attributes
		this.setBaseUrl("");
	}
	
	public UserManager(String baseUrl) {
		// Setup the attributes
		this.setBaseUrl(baseUrl);
	}
	
	public User getUser(int id) {
		// Set the right url
		String url = String.format("%s%s%s%d", this.getBaseUrl(), USER, USER_JSONVIEW, id);
		
		try {
			// Get full content JSON from a url
			JSONObject json = new JSONObject(new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next());
			
			// Create a user object
			User user = new User();
			
			// Set the user properties
			user.setId(json.getJSONObject("User").getInt("id"));
			user.setUserName(json.getJSONObject("User").getString("username"));
			user.setPassword(json.getJSONObject("User").getString("password"));
			user.setRole(json.getJSONObject("User").getString("role"));
			user.setEmail(json.getJSONObject("User").getString("email"));
			user.setQRCode(Base64.decode(json.getJSONObject("User").getString("qrcode"), Base64.DEFAULT));
			user.setCreated(user.loadDate(json.getJSONObject("User").getString("created")));
			user.setModified(user.loadDate(json.getJSONObject("User").getString("modified")));
			
			// Provide the return
			return user;
		} catch (Exception exception) {
			// Log the error
			Log.e("User getUser(int id)", exception.getLocalizedMessage());
						
			// On error
			exception.printStackTrace();
		}
		
		// Provide the return
		return null;
	}
	
	public List<User> getUsers() {
		// Set the right url
		String url = String.format("%s%s%s", this.getBaseUrl(), USER, USER_JSONLIST);
		
		try {
			// Get full content JSON from a url
			JSONArray json = new JSONArray(new Scanner(new URL(url).openStream(), "UTF-8").useDelimiter("\\A").next());
			
			// Create an user list
			List<User> users = new ArrayList<User>();
						
			// Fill the users
			for (int index = 0; index < json.length(); index++) {
				// Create a user object
				User user = new User();
				
				// Set the user properties
				user.setId(json.getJSONObject(index).getJSONObject("User").getInt("id"));
				user.setUserName(json.getJSONObject(index).getJSONObject("User").getString("username"));
				user.setPassword(json.getJSONObject(index).getJSONObject("User").getString("password"));
				user.setRole(json.getJSONObject(index).getJSONObject("User").getString("role"));
				user.setEmail(json.getJSONObject(index).getJSONObject("User").getString("email"));
				user.setQRCode(Base64.decode(json.getJSONObject(index).getJSONObject("User").getString("qrcode"), Base64.DEFAULT));
				user.setCreated(user.loadDate(json.getJSONObject(index).getJSONObject("User").getString("created")));
				user.setModified(user.loadDate(json.getJSONObject(index).getJSONObject("User").getString("modified")));
				
				// Add the user
				users.add(user);
			}
			
			// Provide the return
			return users;
		} catch (Exception exception) {
			// Log the error
			Log.e("List<User> getUsers()", exception.getLocalizedMessage());
						
			// On error
			exception.printStackTrace();
		}
		
		// Provide the return
		return null;
	}	
	
}
